package cn.anc.aonicardv.util.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import cn.anc.aonicardv.R;

/**
 * Created by baobaoding on 2017/5/10.
 */

public class UiTool {
    public static void setTitleString(Activity activity, int stringId) {
        if (activity != null) {
            TextView textView = (TextView) activity.findViewById(R.id.txtTopbarTitle);
            textView.setText(activity.getResources().getString(stringId));
        }
    }

    public static void setTitleString(Activity activity, String title) {
        if (activity != null) {
            TextView textView = (TextView) activity.findViewById(R.id.txtTopbarTitle);
            textView.setText(title);
        }
    }

    public static void setScreenKeepOn(Activity activity) {
        if (activity != null) {
            WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
            attrs.flags &= (~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            activity.getWindow().setAttributes(attrs);
        }
    }

    public static void setCancelScreenKeepOn(Activity activity) {
        if (activity != null) {
            WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
            attrs.flags &= (~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            activity.getWindow().setAttributes(attrs);
        }
    }

    public static void jumpActivity(Activity cur, Class<?> cls) {
        cur.startActivity(new Intent(cur, cls));
    }

    public static void jumpActivity(Activity cur, Class<?> cls, boolean over_this) {
        cur.startActivity(new Intent(cur, cls));
        if (over_this) {
            cur.finish();
        }
    }
    public static String getFileName(String file_path) {
        int start = file_path.lastIndexOf('/');
        return file_path.substring(start + 1);
    }

    public static void setViewSize16x9(Activity activity, View view) {
        if (activity != null) {
            view.getLayoutParams().width = activity.getWindowManager().getDefaultDisplay().getWidth() * 5 >> 3;
            view.getLayoutParams().height = view.getLayoutParams().width * 9 >> 4;
        }
    }

    public static void showToast(Context context, final int str_id) {
        if (context != null && context.getString(str_id) != null) {
            Toast.makeText(context, context.getString(str_id), Toast.LENGTH_LONG).show();
        }
    }

    public static void showToast(Context context,final String str) {
        if (context != null && str != null) {
            Toast.makeText(context, str, Toast.LENGTH_LONG).show();
        }
    }

    public static void showToastShort(Context context,final String str) {
        if (context != null && str != null) {
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }
    }

}
